// pages/staff/chooseCourse/applyfree/applyfree.js
const app = getApp()
var CONFIG = require("../../../../config/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
   stu:[],
   stu_id:'',
   desc:'',
   textfalse: false,
   yue:'',
   buttonclick:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中~',
      mask:true
    })
    // console.log(options);
    this.setData({
      stu_id: options.stu_id
    });
    let that = this;
    //获取课程顾问信息
    wx.request({
      url: CONFIG.API_HOST + 'crm/user/getFreeApply',
      data: {
        user_course_id: that.data.stu_id
      },
      // method: 'GET',
      success: function (res) {
        // console.log(res);
        wx.hideLoading();
        that.setData({
          stu: res.data.data
        })
        // console.log(res.data.data.free_remark);
        if(res.data.data.free_remark){
          that.setData({
            buttonclick:true
          })
          wx.showToast({
            title: '您已申请过~',
          })
        }else{
          that.setData({
            buttonclick:false
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: CONFIG.REQUEST_FAIL,
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },


  // 提交
  applyfreebtn:function(e){
    // console.log(e);
    var e1 = e.detail.value;
    var user_id = this.data.stu_id;
    var user_id1 = this.data.stu.user_id;
    if (e1.textWhy == '') {
      wx.showToast({
        title: '请填写申请原因',
        icon: 'none'
      })
      return false;
    }else{
      wx.showLoading({
        title: '提交中~',
        mask: true
      })
      var formData={
        user_course_id:user_id,
        free_remark:e1.textWhy
      }
      wx.request({
        url: CONFIG.API_HOST + 'crm/user/freeApply',
        data: formData,
        method: 'POST',
        success: function (res) {
          // console.log(res);
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(function(){
            wx.navigateBack();
            // wx.navigateTo({
            //   url: '/pages/public/userCourses?user_id='+user_id1+'',
            // })
          },1000)
        },
        fail: function (res) {
          wx.hideLoading();
          wx.showModal({
            title: CONFIG.MODEL_TITLE,
            showCancel: false,
            content: CONFIG.REQUEST_FAIL,
            confirmText: '点击重试',
            success: function (res) {
              wx.navigateBack();
            }
          })
        }
      })
    }

  }
})